package com.example.plugin.ui;

import com.example.plugin.config.PanelModel;
import com.example.plugin.setting.GenerateSetting;
import com.example.plugin.setting.Settings;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.border.CustomLineBorder;
import com.intellij.ui.components.JBList;

import javax.swing.*;
import java.awt.*;
import java.util.Set;

/**
 * history面板
 *
 * @author lipan
 * @date 2022/1/16 15:25
 * @email 13160080997@163.com
 **/
public class HistoryPanel extends JPanel {

    private GenerateUI generateUI;

    public GenerateUI getGenerateUI() {
        return generateUI;
    }

    public void setGenerateUI(GenerateUI generateUI) {
        this.generateUI = generateUI;
    }

    //历史面板下拉
    private JBList<String> itemList;

    public HistoryPanel(GenerateUI generateUI, Set<String> data) {
        this.generateUI = generateUI;
        this.setLayout(new BorderLayout());

        // 头部操作按钮
        JPanel headToolbar = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        // 添加事件按钮
        DefaultActionGroup actionGroup = createActionGroup();

        ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar("Item Toolbar", actionGroup, true);

        headToolbar.add(actionToolbar.getComponent());
        // 添加边框
        actionToolbar.getComponent().setBorder(new CustomLineBorder(1, 1, 1, 1));
        this.add(actionToolbar.getComponent(), BorderLayout.NORTH);

        itemList = new JBList(new CollectionListModel(data.toArray(new String[0])));
        itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        itemList.setPreferredSize(new Dimension(-1,-1));
        itemList.addListSelectionListener( listener -> {
            String selectedValue = itemList.getSelectedValue();
            PanelModel selectedItem = getSelectedItem(selectedValue);
            if(null==selectedItem){
                return;
            }
            setValue(selectedItem,generateUI);
        });
        this.add(itemList);
        setPreferredSize(new Dimension(160,430));
    }

    private PanelModel getSelectedItem(String select){
        Settings settings = GenerateSetting.settings;
        return settings.getPanelModel(select);
    }

    private void setValue(PanelModel panelModel,GenerateUI generateUI){
        generateUI.getEntityTextField().setText(panelModel.getEntityName());
        generateUI.getTableTextField().setText(panelModel.getTableName());
        generateUI.getDaoFolderBtn().setText(panelModel.getDaoPath());
        generateUI.getServiceFolderBtn().setText(panelModel.getServicePath());
        generateUI.getControllerFolderBtn().setText(panelModel.getControllerPath());
        generateUI.getXmlFolderBtn().setText(panelModel.getXmlPath());
        generateUI.getEntityFolderBtn().setText(panelModel.getEntityPath());

        generateUI.getDaoCheck().setSelected(panelModel.isDaoCheck());
        generateUI.getServiceCheck().setSelected(panelModel.isServiceCheck());
        generateUI.getControllerCheck().setSelected(panelModel.isControllerCheck());
        generateUI.getEntityCheck().setSelected(panelModel.isEntityCheck());
        generateUI.getXmlCheck().setSelected(panelModel.isXmlCheck());

    }

    /**
     * 创建动作组
     *
     * @return 动作组
     */
    private DefaultActionGroup createActionGroup() {
        DefaultActionGroup actionGroup = new DefaultActionGroup();
        // 删除事件
        actionGroup.add(new AnAction(AllIcons.General.Remove) {
            @Override
            public void actionPerformed(AnActionEvent e) {
                //移除缓存
                String selectedValue = itemList.getSelectedValue();
                GenerateSetting.settings.getPanelModelMap().remove(selectedValue);
                CollectionListModel<String> models = (CollectionListModel<String>) itemList.getModel();
                models.remove(selectedValue);

            }
        },Constraints.LAST);
        return actionGroup;
    }

}
